<?php

namespace Sharks\Core\LifeCycle;

use Sharks\Core\Cfg\AppConfig;
use Sharks\Core\Support\Log\Log;

class ResolverLoader {

    public static function load(array $defaults, $configItemName = null) {
        if (is_null($configItemName)) {
            return self::initialize($defaults);
        }
        $resolvers = AppConfig::get($configItemName, $defaults);
        $resolvers =  array_merge($resolvers, $defaults);
        $resolvers = array_unique($resolvers);
        return self::initialize($resolvers);
    }

    private static function initialize(array $resolverClassNames) {
        $result = array();
        foreach ($resolverClassNames as $re) {
            if (class_exists($re)) {
                $result[] = new $re();
            }
            else {
                Log::warning("$re 不存在");
            }
        }
        return $result;
    }
}